package com.start_eternity.oasissafewalk.controller;

import com.start_eternity.oasissafewalk.comment.ErrorCode;
import com.start_eternity.oasissafewalk.comment.ResultUtils;
import com.start_eternity.oasissafewalk.pojo.OrderDates;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.service.OrderDatesService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * author:ZhangT
 * Date:2024 / 03 / 30 / 16:00
 * Description:
 */
@Slf4j
@RestController
@RequestMapping("orderDates")
public class OrderDatesController {

    @Resource
    private OrderDatesService orderDatesService;

    //根据Id查询
    @GetMapping("/checkId")
    public Result<OrderDates> checkId(@RequestParam Integer id) {
        OrderDates query = orderDatesService.query(id);
        if (query != null) {
            return ResultUtils.success(query);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //订单天数的添加
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody OrderDates orderDates) {
        int i = orderDatesService.saveOrderDates(orderDates);
        if (i == 0) {
            ResultUtils.error(ErrorCode.FAIL_INSERT);
        }
        return ResultUtils.success(i);
    }

    //修改订单天数
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody OrderDates orderDates) {
        int i = orderDatesService.changeNumDays(orderDates);
        if (i == 0) {
            return ResultUtils.error(ErrorCode.FAIL_UPDATE);
        }
        return ResultUtils.success(i);
    }
}
